// var baseUrl = 'http://119.45.1.190:8085';
var baseUrl = 'https://app.yueluo.club/5gmaiquan'
var apiList = require('./apiList')

function http(url, method, data, contentT) {
  wx.showLoading({
    title: "加载中",
    mask: true
  })
  var contentType = 'application/x-www-form-urlencoded';
  if (contentT) {
    contentType = contentT
  } else {
    contentType = contentType
  }
  return new Promise(function(resolve, reject) {
    wx.request({
      url: baseUrl + url, 
      data: data,
      method: method,
      header: {
        'Content-type': contentType
      },
      success(res) {
        wx.hideLoading()
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

//登录接口的调用
var login = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.login, 'POST', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//注册接口调用
var register = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.register, 'POST', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//新增接口
var taskAdd = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.taskAdd, 'POST', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//获取任务列表
var getAll = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.getAll, 'GET', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//获取任务列表里图片
var getImg = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.download, 'GET', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//领取任务
var accept = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.accept, 'POST', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//待完成列表
var orderGetAll = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.orderGetAll, 'GET', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//取消待完成里某项任务 
var orderRemove = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.orderRemove, 'POST', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//提交待完成里任务
var orderSubmit = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.orderSubmit, 'POST', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//审核
var orderAudit = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.orderAudit, 'POST', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//审核列表
var orderGetAuditAll = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.orderGetAuditAll, 'GET', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//审核详情接口
var getAuditInfo = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.getAuditInfo, 'GET', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//管理页用户列表
var userList = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.userList, 'POST', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}


//管理页用户删除
var userDelete = (data) => {
  return new Promise((resolve, reject) => {
    http(apiList.userDelete, 'GET', data)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

//管理页修改用户等级
var userEdit = (url,data) => {
  return new Promise((resolve, reject) => {
    http(apiList.userEdit+url, 'POST', data, 'application/json')
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}


module.exports = {
  login,
  register,
  taskAdd,
  getAll,
  getImg,
  accept,
  orderGetAll,
  orderRemove,
  orderSubmit,
  orderAudit,
  orderGetAuditAll,
  getAuditInfo,
  userList,
  userDelete,
  userEdit
}